#!/usr/bin/python
# -*- coding:utf-8 -*-
import json
from flask import Flask, url_for, request, redirect, jsonify, render_template
from functions.rbac import *

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/login')
def _login():
    return render_template('login.html')


@app.route('/test')
def _test():
    return render_template('test.html')


@app.route('/app1')
def _app1():
    return render_template('app1.html')


@app.route('/app2')
def _app2():
    return render_template('app2.html')


@app.route('/adminLogin', methods=['POST'])
def _adminLogin():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        token = adminLogin(username, password)
        if token is not None:
            result = token.copy()
            result.update({'result': True})
            return json.dumps(result)
    return json.dumps({'error': 'error'})


@app.route('/adminCheckLogin', methods=['POST'])
def _adminCheckLogin():
    if request.method == 'POST':
        username = request.form['username']
        token = request.form['token']
        return json.dumps({'result': adminCheckLogin(username, token)})


@app.route('/userLogin', methods=['POST'])
def _userLogin():
    username = request.form['username']
    password = request.form['password']
    token = login(username, password)
    if token is not None:
        result = token.copy()
        result.update({'result': True})
        return json.dumps(result)
    return json.dumps({'result': False})


@app.route('/checkLogin', methods=['POST', 'GET'])
def _checkLogin():
    callback = request.args.get('callback')
    username = request.args.get('username')
    token = request.args.get('token')
    return callback + '(' + json.dumps({'result': checkLogin(username, token)}) + ')'


@app.route('/getIdAndUsers', methods=['POST'])
def _getUsers():
    return json.dumps(getIdAndUsers())


@app.route('/deleteUser', methods=['POST'])
def _deleteUser():
    userId = request.form['userId']
    deleteUser(userId)
    return json.dumps({'result': True})


@app.route('/addUser', methods=['POST'])
def _addUser():
    username = request.form['username']
    passowrd = request.form['password']
    addUser(username, passowrd)
    return json.dumps({'result': True})


@app.route('/getIdAndRoles', methods=['POST'])
def _getRoles():
    return json.dumps(getIdAndRoles())


@app.route('/setRole', methods=['POST'])
def _setRole():
    userId = request.form['userId']
    roleId = request.form['roleId']
    return json.dumps({'result': setRole(userId, roleId)})


@app.route('/deleteRole', methods=['POST'])
def _deleteRole():
    roleId = request.form['roleId']
    deleteRole(roleId)
    return json.dumps({'result': True})


@app.route('/addRole', methods=['POST'])
def _addRole():
    name = request.form['name']
    content = request.form['content']
    return json.dumps({'result': addRole(name, content)})


@app.route('/setLevel', methods=['POST'])
def _setLevel():
    highId = request.form['highId']
    lowId = request.form['lowId']
    return json.dumps({'result': setLevel(highId, lowId)})


@app.route('/getIdAndAuthorities', methods=['POST'])
def _getIdAndAuthorities():
    return json.dumps(getIdAndAuthorities())


@app.route('/setAuthority', methods=['POST'])
def _setAuthority():
    roleId = request.form['roleId']
    authorityId = request.form['authorityId']
    return json.dumps({'result': setAuthority(roleId, authorityId)})


@app.route('/deleteLevel', methods=['POST'])
def _deleteLevel():
    highId = request.form['highId']
    lowId = request.form['lowId']
    deleteLevel(highId, lowId)
    return json.dumps({'result': True})


@app.route('/getLowIdAndRoleByHighId', methods=['POST'])
def _getLowIdAndRoleByHighId():
    highId = request.form['highId']
    return json.dumps(getLowIdAndRoleByHighId(highId))


@app.route('/deleteRoleAuthority', methods=['POST'])
def _deleteRoleAuthority():
    roleId = request.form['roleId']
    authorityId = request.form['authorityId']
    deleteRoleAuthority(roleId, authorityId)
    return json.dumps({'result': True})


@app.route('/getIdAndAuthoritiesByRoleId', methods=['POST'])
def _getIdAndAuthoritiesByRoleId():
    roleId = request.form['roleId']
    return json.dumps(getIdAndAuthoritiesByRoleId(roleId));


@app.route('/setIncompatibles', methods=['POST'])
def _setIncompatibles():
    oneId = request.form['oneId']
    anotherId = request.form['anotherId']
    print oneId
    print anotherId
    return json.dumps({'result': setIncompatibles(oneId, anotherId)})


@app.route('/addAuthority', methods=['POST'])
def _addAuthority():
    name = request.form['name']
    return json.dumps({'result': addAuthority(name)})


@app.route('/deleteAuthority', methods=['POST'])
def _deleteAuthority():
    authorityId = request.form['authorityId']
    deleteAuthority(authorityId)
    return json.dumps({'result': True})


@app.route('/getIdAndUsersP', methods=['GET'])
def _getIdAndUsersP():
    username = request.args.get('username')
    appFlag = request.args.get('appFlag')
    callback = request.args.get('callback')
    return callback + '(' + json.dumps(getIdAndUserByUsername(username, appFlag)) + ')'


@app.route('/logout', methods=['GET'])
def _logout():
    username = request.args.get('username')
    callback = request.args.get('callback')
    logout(username)
    return callback + '(' + json.dumps({}) + ')'


@app.route('/auditing')
def _auditing():
    return render_template('auditing.html');


@app.route('/getRBACLogs', methods=['POST'])
def _getRBACLogs():
    size = request.form['size']
    operation = request.form['operation']
    return json.dumps(getRBACLogs(int(size), int(operation)))


@app.route('/getAppLogs', methods=['POST'])
def _getAppLogs():
    size = request.form['size']
    operation = request.form['operation']
    return json.dumps(getAppLogs(int(size), int(operation)))


@app.route('/deleteLogs', methods=['POST'])
def _deleteLogs():
    deleteApp = request.form['deleteApp']
    time = request.form['time']
    print deleteApp
    print time
    deleteLogs(deleteApp, time)
    return json.dumps({})


if __name__ == '__main__':
    app.debug = True
    app.run()